package com.gdqy.FCS.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.Fileupload;
import com.gdqy.FCS.entity.Brand;
import com.gdqy.FCS.entity.Clothing;
import com.gdqy.FCS.entity.Comment;
import com.gdqy.FCS.entity.Design;
import com.gdqy.FCS.entity.Dictionary;
import com.gdqy.FCS.entity.EnterpriseInfo;
import com.gdqy.FCS.entity.Fabric;
import com.gdqy.FCS.entity.FabricApplication;
import com.gdqy.FCS.entity.FabricComponent;
import com.gdqy.FCS.entity.FabricProduction;
import com.gdqy.FCS.entity.ProviderInfo;
import com.gdqy.FCS.entity.User;
import com.gdqy.FCS.entity.Weave;
import com.gdqy.FCS.service.BrandService;
import com.gdqy.FCS.service.ClothingService;
import com.gdqy.FCS.service.CommentService;
import com.gdqy.FCS.service.DesignService;
import com.gdqy.FCS.service.DictionaryService;
import com.gdqy.FCS.service.EnterpriseInfoService;
import com.gdqy.FCS.service.FabricApplicationService;
import com.gdqy.FCS.service.FabricComponentService;
import com.gdqy.FCS.service.FabricProductionService;
import com.gdqy.FCS.service.FabricService;
import com.gdqy.FCS.service.ProviderInfoService;
import com.gdqy.FCS.service.UserService;
import com.gdqy.FCS.service.WeaveService;
import com.sun.mail.handlers.message_rfc822;


@Controller
@RequestMapping("fabric")
public class FabricController {

	@Autowired
	private FabricService fabricService;
	@Autowired
	private FabricComponentService fabricComponentService;
	@Autowired
	private WeaveService weaveService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private DesignService designService;
	@Autowired
	private ClothingService clothingService;
	@Autowired
	private EnterpriseInfoService enterpriseInfoService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private UserService userService;
	@Autowired
	private ProviderInfoService providerInfoService;
	@Autowired
	private FabricApplicationService fabricApplicationService;
	@Autowired
	private FabricProductionService fabricProductionService;
	
	
	/***********************************************************************************************************/
	@RequestMapping("deal_addFabric")
	public ModelAndView deal_addFabric(@RequestParam(value = "provider_id", required = true) int provider_id) {
		ModelAndView modelAndView = new ModelAndView();
		//成分大类
		List<FabricComponent> fabricComponents = fabricComponentService.selectClass("");
		modelAndView.addObject("fabricComponents", fabricComponents);
		//织法大类
		List<Weave> weaves = weaveService.selectByParent_code("");
		modelAndView.addObject("weaves", weaves);
		//制作工艺
		List<Dictionary> FP_dic = dictionaryService.selectByType_code("ZD008");
		modelAndView.addObject("FP_dic", FP_dic);
		//主要用途
		List<Dictionary> FA_dic = dictionaryService.selectByType_code("ZD007");
		modelAndView.addObject("FA_dic", FA_dic);
	
		modelAndView.setViewName("provider/addFabric");
		return modelAndView;
	}
	
	@RequestMapping("addFabric")
	public ModelAndView addFabric(Fabric fabric, MultipartFile photo_pic1, MultipartFile photo_pic2,
			MultipartFile photo_pic3, MultipartFile photo_pic4, MultipartFile photo_pic5,
			String FA_code,String FP_code) throws IllegalStateException, IOException {
		ModelAndView modelAndView = new ModelAndView();
		String msg = "上传失败";
		String fabric_name = fabric.getName();
		if(fabric_name.equals("") || fabric_name ==null) {
			msg = "上传失败";
			modelAndView.addObject("msg", msg);
			modelAndView.setViewName("provider/addFabric");
			return modelAndView;
		}
		
		ProviderInfo providerInfo = providerInfoService.selectById(fabric.getProvider_id());
		User user = userService.selectById(providerInfo.getUser_id());
		String role = user.getRole();
		String username = user.getUsername();
		String type = "fabrics";
		Fileupload fileupload = new Fileupload();
		String photo_url1 = fileupload.upload(photo_pic1, role, username, type);
		String photo_url2 = fileupload.upload(photo_pic2, role, username, type);
		String photo_url3 = fileupload.upload(photo_pic3, role, username, type);
		String photo_url4 = fileupload.upload(photo_pic4, role, username, type);
		String photo_url5 = fileupload.upload(photo_pic5, role, username, type);
		fabric.setPhoto_url1(photo_url1);
		fabric.setPhoto_url2(photo_url2);
		fabric.setPhoto_url3(photo_url3);
		fabric.setPhoto_url4(photo_url4);
		fabric.setPhoto_url5(photo_url5);
		fabric.setLiked(0);
		fabric.setCollected(0);
		fabric.setRelease_time(new Date());
		fabric.setUpdate_time(new Date());
		fabric.setStatus(1);
		int data = fabricService.insert(fabric);
		if(data>0) {
			int fabric_id = fabric.getId();
			FabricApplication fabricApplication = new FabricApplication();
			fabricApplication.setFabric_id(fabric_id);
			fabricApplication.setProduction_code(FA_code);
			fabricApplicationService.insert(fabricApplication);
			FabricProduction fabricProduction = new FabricProduction();
			fabricProduction.setFabric_id(fabric_id);
			fabricProduction.setProduction_code(FP_code);
			fabricProductionService.insert(fabricProduction);
			msg = "上传成功";
		} else {
			msg = "上传失败";
		}
		modelAndView.addObject("msg", msg);
		modelAndView.setViewName("provider/addFabric");
		return modelAndView;
	}
	
	/************************************************************************************************************/
	
	/*
	 * 
	 * 返回面料首页视图
	 * 
	 * */
	@RequestMapping("getFabricMain")
	public ModelAndView getFabricList() {
		ModelAndView modelAndView = new ModelAndView();
		
		Fabric fabric=new Fabric();
		fabric.setCollected(0);
		//人气面料 
		List<Fabric> collectedList=fabricService.selectAll(fabric);
		
		//最新面料
		fabric.setCollected(null); 
		fabric.setRelease_time(new Date());
		List<Fabric> newList=fabricService.selectAll(fabric);	
		
		//面料成分
		List<FabricComponent> componentList=fabricComponentService.selectAll();
		//面料用途
		List<Dictionary> applicationList=dictionaryService.selectByType_code("ZD007");
		//面料工艺
		List<Dictionary> productionList=dictionaryService.selectByType_code("ZD008");
		//面料织法
		List<Weave> weaveList=weaveService.selectAll();
		
		int fabricCount = fabricService.selectAllCount();
		modelAndView.addObject("productionList",productionList);
		modelAndView.addObject("weaveList",weaveList);
		modelAndView.addObject("applicationList",applicationList);
		modelAndView.addObject("collectedList",collectedList);
		modelAndView.addObject("newList",newList);
		modelAndView.addObject("componentList",componentList);		
		modelAndView.addObject("fabricCount",fabricCount);	
		modelAndView.setViewName("fabric/fabric");

		return modelAndView;

	}

	/*
	 * 
	 * 通过id获取面料详情
	 * 
	 * */
	@RequestMapping("getFabricById")
	public ModelAndView getFabricById(String id, HttpServletRequest request,Comment com) {
		
		ModelAndView modelAndView = new ModelAndView();
		//通过id拿到面料
		Fabric fabric=fabricService.selectById(Integer.parseInt(id));
		//通过面料编号拿到匹配设计作品
		//分页匹配作品
				List<Design> designList=designService.selectByFabric_code(fabric.getComponent_code());

		//通过面料编号拿到匹配服装
		List<Clothing> clothingList=clothingService.selectByFabric_code(fabric.getComponent_code());
//		HashMap<EnterpriseInfo, List<EnterpriseInfo>> map = new HashMap<EnterpriseInfo, List<EnterpriseInfo>>();
		//通过服装拿到服装企业
		List<EnterpriseInfo> enterpriseInfoList = new  ArrayList<EnterpriseInfo>();
		List<Brand> brandList=new ArrayList<Brand>();
		for(Clothing clothing  : clothingList){
			EnterpriseInfo enterprise=enterpriseInfoService.selectById(clothing.getEnterprise_id());
			enterpriseInfoList.add(enterprise);
			Brand brand=brandService.selectById(clothing.getEnterprise_id());
			brandList.add(brand);
		}
		//通过设计作品拿到款式
		List<Dictionary> patternList=new ArrayList<Dictionary>();
		for(Design design : designList){
			Dictionary pattern=dictionaryService.selectByCode(design.getPattern_code());
			patternList.add(pattern);
		}

		
		
		//面料评论
		List<Comment> comments = commentService.selectByObj_idAndType(Integer.parseInt(id), "3");
		
		//	modelAndView.addObject(patternList);
		request.setAttribute("patternList", patternList);
		modelAndView.addObject(brandList);
		modelAndView.addObject(enterpriseInfoList);
		modelAndView.addObject(fabric); 
		modelAndView.addObject(designList); 
		modelAndView.addObject(clothingList); 
	//	modelAndView.addObject(comments);
		request.setAttribute("comments", comments);
		modelAndView.setViewName("fabric/fabricDetail");
		return modelAndView;
	}

	/*
	 * 
	 * 通过id删除面料
	 * 
	 * */
	@RequestMapping("deleteById")
	public ModelAndView deleteById(String id) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("index");
		
		int count = fabricService.deleteById(Integer.parseInt(id));
		modelAndView.addObject(count);
		 
		return modelAndView;

	}	

	/*
	 * 
	 * 修改面料信息
	 * 
	 * */
	@RequestMapping("updateFabric")
	public ModelAndView deleteById(Fabric fabric) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("index");
		
		int count = fabricService.update(fabric);
		modelAndView.addObject(count);
		 
		return modelAndView;

	}	
	
	/*
	 * 
	 * 新增面料
	 * 
	 * */
	@RequestMapping("insertFabric")
	public ModelAndView insertFabric(Fabric fabric) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("index");
		
		int count = fabricService.insert(fabric);
		modelAndView.addObject(count);
		 
		return modelAndView;

	}
	
	/*
	 * 
	 * 返回面料列表视图
	 * 
	 * */
	
	@RequestMapping("getFabricList")
	public ModelAndView getFabricListContain(String code,int i) {
		ModelAndView modelAndView = new ModelAndView();
		
		Fabric fabric=new Fabric();
		fabric.setCollected(0);
		//面料成分
		List<FabricComponent> componentList=fabricComponentService.selectAll();
		//面料用途
		List<Dictionary> applicationList=dictionaryService.selectByType_code("ZD007");
		//面料工艺
		List<Dictionary> productionList=dictionaryService.selectByType_code("ZD008");
		//面料织法
		List<Weave> weaveList=weaveService.selectAll();
		List<FabricApplication> fabricappList=new ArrayList<FabricApplication>();
		List<FabricProduction> fabricproductionList=new ArrayList<FabricProduction>();
		List<Fabric> fabricList=new ArrayList<Fabric>();
		//根据相关分类拿到成分面料
		Map<String, String> map=new HashMap<String, String>();
		if(i ==1){
			map.put("component_code", code);
			fabricList=fabricService.selectByComAndWeaCode(map);
		}else if(i == 2){
			map.put("weave_code", code);
			fabricList=fabricService.selectByComAndWeaCode(map);
		}else if(i ==3){
			fabricappList=fabricService.selectFabricByAppCode(code);
			for(FabricApplication fa:fabricappList)
				fabricList.add(fa.getFabric());
		}else if(i ==4){
			fabricproductionList=fabricService.selectFabricByProCode(code);
			for(FabricProduction fa:fabricproductionList)
				fabricList.add(fa.getFabric());
		}
	modelAndView.addObject("productionList",productionList);
		modelAndView.addObject("weaveList",weaveList);
	modelAndView.addObject("applicationList",applicationList);
		modelAndView.addObject("componentList",componentList);
		
//		//通 过code查询面料--分页返回
		modelAndView.addObject("fabricList",fabricList);
		
	modelAndView.setViewName("fabric/fabricList");
	
	return modelAndView;

	}
	

}
